<template>
    <div></div>
</template>
<script>
    // const socket = ["183.67.23.135", "bytest", "123456"];
    // const socket = ["172.21.1.5", "bytest", "123456"];
    // const socket = ["222.180.59.158", "bytest", "123456"];
    const socket = ["192.168.1.121", "bytest", "123456"];
    // const socket = ["192.168.1.56", "bytest", "123456"];
    // const socket = ["192.168.1.200", "bytest", "123456"];
    // const socket = ["192.168.1.200", "bytest", "123456"];
    // require("../../../static/mqttws31.js");
    // require("../../../static/mqttmq.js");
    // require('../../utils/mqttmq.js')
    // require('../../utils/mqttws31.js')
    export default {
        props: {
            destination: {
                type: String,
                required: true
            },
            qos: {
                type: Number,
                default: 1
            },
            name: {
                type: String,
                default: 'zl'
            }
        },
        data() {
            return {
                connecting: false // 为true则持续请求
            };
        },
        created() {
            // if (!window.WebSocket) {
            //     console.log("不支持websocket!!!");
            // } else {
            //     this.loadData()
            // }
            // console.log("开始");
        },
        methods: {
            loadData() {
                this.connecting = true;
                this.connect();
                let self = this
                return new Promise((resolve, reject) => {
                    MqttMq.connect(
                        ...socket,
                        this.destination,
                        function onConnect(frame) {
                            console.info("连接成功");
                            //设置为持久订阅，且Qos级别为1
                            MqttMq.client.subscribe(MqttMq.destination, {
                                qos: self.qos
                            });
                            self.$emit('connect-success')
                            resolve()
                        },
                        this.onFailure,
                        this.onMessageArrived,
                        this.onConnectionLost
                    );
                });
            },
            // 开始连接
            connect() {
                MqttMq.connect(
                    ...socket,
                    this.destination,
                    this.onConnect,
                    this.onFailure,
                    this.onMessageArrived,
                    this.onConnectionLost
                );
            },
            onConnect(frame) {
                let self = this
                console.log(this.name);
                console.info(this.name + ':' + "连接成功");
                //设置为持久订阅，且Qos级别为1
                MqttMq.client.subscribe(MqttMq.destination, {
                    qos: self.qos
                });
            },
            onFailure(failure) {
                console.info("连接失败");
                console.info(failure.errorMessage);
                this.$emit("failure", failure);
                // 连接失败,重新连接
                if (this.connecting) {
                    console.log('222');
                    this.connect();
                }
            },
            // 消息模块
            onMessageArrived(message) {
                // console.log(message);
                // console.log(message.payloadString);
                this.$emit("message", message);
                // console.log(message.payloadString.indexOf('[') !== -1)
                message.payloadString.indexOf('[') !== -1 ? this.$emit("payload", JSON.parse(message.payloadString)) : null
            },
            // 连接丢失
            onConnectionLost(responseObject) {
                if (responseObject.errorCode !== 0) {
                    console.info(
                        MqttMq.client.clientId + ": " + responseObject.errorCode
                    );
                }
                // 连接丢失,重新连接
                console.log(this.name + ':' + this.connecting);
                if (this.connecting) {
                    console.log('111');
                    this.connect();
                }
            },
            // 主动断开
            disConnect() {
                console.log('已断开');
                this.connecting = false;
                console.log(this.name + ':' + this.connecting);
                MqttMq.disconnect();
            }
        },
        destroyed() {
            // 销毁则断开
            console.log("websocket销毁");
            this.disConnect()
        },
        deactivated() {
            // 销毁则断开
            console.log("websocket销毁");
            this.disConnect()
        }
    };
</script>
<style lang="scss" scoped>
</style>
